import { defineStore } from "pinia";

export const useTab = defineStore("tabList", {
  state: () => ({
    tabList: [
      { name: "首页", path: "/home", icon: "HomeOutlined", isAffix: true },
    ], // 标签列表
    activeTab: "", // 当前激活的标签
  }),
  actions: {
    //添加tab
    addTab(tab) {
      const filterTab = this.tabList.filter((item) => {
        return item.path === tab.path;
      });
      if (filterTab.length === 0) {
        this.tabList.push(tab);
      }
      this.activeTab = tab.path;
    },
    //删除tab
    removeTab(key) {
      const tabList = this.tabList;
      const removeIndex = tabList.findIndex((item) => {
        return item.path === key;
      });
      tabList.splice(removeIndex, 1);
      this.activeTab =
        tabList[removeIndex]?.path || tabList[removeIndex - 1]?.path || "/home";
    },
  },
  persist: {
    key: "tabList",
    storage: localStorage,
  },
});
